package com.feiyi.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.feiyi.app.R;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.PayResult;
import com.feiyi.app.utils.OrderInfoUtil2_0;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author wangliantao
 * @time 2018 12 2018/12/10 1:21 AM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： In me the tiger sniffs the rose
 */
public class GoToPayActivity extends BaseActivity {

    Unbinder unbinder;

    @BindView(R.id.tv_title_titlebar)
    TextView tvTitleBar;
    @BindView(R.id.tv_amount_pay)
    TextView tv_amount_pay;
    private String price;

    private static final int SDK_PAY_FLAG = 1001;
    private String RSA_PRIVATE = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCNYm+oveZOECAjwrH1E+RHznGxVqdAKI/teijarKYIV7RjpNyfMaEaI0ms8vd9aXtN6gEeSPvBQmWVunY1FWfLpAOkSYGJLJ8GJEgiNTAstCgkHw21DaojrD9LxoUZbvfBwWXiDLDAPUGiU6pnG7AkClJuzSETMCTWsrcB35Y9MMprnPaXgNG8+MJ6P2Z1xmN51uNQw4Z99iDrR27lrQH/OXNzLnRDzlj0rwoYFHDSPds58qmjVRTcBXCVpZoLmuf4OfSc8gplNGz/qs/rjOfKEOrcZQeKw1SCkG5U4ZHsMM5XmwbCGg20G9+BokYdHJNKFKu/+kwu69No1Mcy8RTfAgMBAAECggEAIXBCkFo5egT+VPbbN+d4ejMtWI/yBo6RW80klHN44Ug89cQsGcqXG6N07V6ZgiPMceUCVrNUN6UIeZ0cD/n8DoHACr8Hz/Wptr4mAVErD6ecRs7BYyzULJO0dKuDFzzThBPFkO0HcLAMMeQvzSsTQbLfRC1nw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

    public static final String ALIPAY_SHOP = "https://qr.alipay.com/stx05107r5oaa4fyofbkh24";//商户
    public static final String ALIPAY_PERSON = "HTTPS://QR.ALIPAY.COM/FKX06148QMZIJDXGPKXXE7";//个人(支付宝里面我的二维码)
    public static final String ALIPAY_PERSON_2_PAY = "HTTPS://QR.ALIPAY.COM/FKX01415BIHINQT6TRU53F";//个人(支付宝里面我的二维码,然后提示让用的收款码)


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg); switch (msg.what) {
            case SDK_PAY_FLAG:
                PayResult payResult = new PayResult((Map<String, String>) msg.obj); //同步获取结果
                String resultInfo = payResult.getResult();
//                Log.i("Pay", "Pay:" + resultInfo);
                String resultStatus = payResult.getResultStatus();
                // 判断resultStatus 为9000则代表支付成功
                if (TextUtils.equals(resultStatus, "9000")) {
                    Toast.makeText(activity, "支付成功", Toast.LENGTH_SHORT).show();
                } else { Toast.makeText(activity, "支付失败", Toast.LENGTH_SHORT).show(); }
                break;
        }
        }
    };



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_go_to_pay);
        unbinder = ButterKnife.bind(this);
        tvTitleBar.setText("请支付");
        price = getIntent().getStringExtra("price");
        tv_amount_pay.setText(price);
    }
    @OnClick({R.id.ll_pay_alipay,R.id.ll_pay_wechat})
    public void onClick(View view){
        if (view.getId() == R.id.ll_pay_alipay){
            //秘钥验证的类型 true:RSA2 false:RSA
             boolean rsa = false;
             //构造支付订单参数列表
             Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa);
             //构造支付订单参数信息
             String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
             //对支付参数信息进行签名
             String sign = OrderInfoUtil2_0.getSign(params, RSA_PRIVATE, rsa);
             //订单信息
             final String orderInfo = orderParam + "&" + sign;
             //异步处理
             Runnable payRunnable = new Runnable() {
                 @Override public void run() {
                     //新建任务
                      PayTask alipay = new PayTask(activity);
                      //获取支付结果
                      Map<String, String> result = alipay.payV2(orderInfo, true);
                      Message msg = new Message();
                      msg.what = SDK_PAY_FLAG;
                      msg.obj = result;
                      mHandler.sendMessage(msg); }
             };
             // 必须异步调用
             Thread payThread = new Thread(payRunnable);
             payThread.start();

        }else if (view.getId() ==R.id.ll_pay_wechat){

        }
    }
}
